/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestMessageFormat.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.text;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;

/**
 * java.text.MessageFormat测试<br/>
 * 
 * 模式字符串: On {0}, {1} destroyed {2} houses and caused {3} of damage<br/>
 * 对模式字符串进行格式化操作时, 需要采用数组的方式提供模式字符串中的每个占位符所对应的参数<br/>
 * 
 * 占位符有以下三种方式:<br/>
 * 1. {argumentIndex}: 0-9之间的数字, 表示要格式化对象数据在参数数组中的索引号<br/>
 * 2. {argumentIndex, formatType}: 参数的格式化类型<br/>
 * 3. {argumentIndex, formatType, FormatStyle}: 与指定的格式化类型对应的模式,
 * 它的值必须是与相应的格式化类型匹配的合法模式或表示合法模式的字符串<br/>
 * 
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestMessageFormat {

	/**
	 * 测试MessageFormat
	 */
	public static void testMessageFormat1() {
		String message = "Today is {0} and the weather is {1}";

		Date today = new Date();
		String weather = "good";

		String formattedMessage = MessageFormat.format(message, today, weather);
		System.out.println(formattedMessage);

		// 可以使用已经格式化好的数据
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
		String formattedDate = dateFormat.format(today);

		System.out.println(MessageFormat.format(message, formattedDate, weather));
	}

	public static void testMessageFormat2() {
		String pattern = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";
		MessageFormat messageFormat = new MessageFormat(pattern, Locale.CHINA);
		Object[] params = { new Date(), 99, 1000000 };
		String result = messageFormat.format(params);
		System.out.println(result);
	}

	public static void testMessageFormat3() {
		String pattern = "On {0, time, short}, a hurricance destroyed {1, number} houses and caused {2, number, currency} of damage.";
		MessageFormat messageFormat = new MessageFormat(pattern, Locale.US);
		Object[] params = { new Date(), 99, 1000000 };
		String result = messageFormat.format(params);
		System.out.println(result);
	}

	public static void main(String[] args) {
		testMessageFormat1();
		testMessageFormat2();
		testMessageFormat3();
	}
}
